package com.meshnetics.gb.stack.mac.type;

import com.meshnetics.gb.type.DataValue;
import com.meshnetics.gb.type.impl.custom.CollectionDataType;
import com.meshnetics.gb.type.impl.zcl.TypeUInt;

import java.nio.ByteBuffer;

/**
 * WRITE DESCRIPTION HERE
 *
 * @author AKovylin
 */
public class TypeBeaconAddrList extends CollectionDataType {
    public static final TypeBeaconAddrList TYPE = new TypeBeaconAddrList();

    TypeBeaconAddrList() {
        super("beaconAddrList");
    }

    public DataValue fromObject(Object value) {
        throw new UnsupportedOperationException("not implemented yet");
        // todo reimplement
    }

    public DataValue deserialize(ByteBuffer bb) {
        throw new UnsupportedOperationException(
                "unconditinal deserialize impossible, PendingAddressSpec must be provided");
    }

    public DataValue deserialize(ByteBuffer bb, PendingAddressSpec condition) {
        DataValue[] elements =
                new DataValue[condition.getShortAddressesPending() + condition.getExtendedAddressesPending()];
        for (int i = 0; i < condition.getShortAddressesPending(); i++) {
            elements[i] = TypeUInt.UINT_16.deserialize(bb);
        }
        for (int i = condition.getShortAddressesPending(); i < elements.length; i++) {
            elements[i] = TypeUInt.UINT_64.deserialize(bb);
        }
        return new BeaconAddrList(elements);
    }
}
